package cn.com.yto56.coresystem.logic.msg.mc.dao.impl;

import cn.com.yto56.coresystem.common.msg.util.SysConst;
import cn.com.yto56.coresystem.common.msg.util.base.StringUtils;
import cn.com.yto56.coresystem.common.msg.util.data.DataTypeChangeTool;
import cn.com.yto56.coresystem.common.msg.util.db.BaseJdbcUtil;
import cn.com.yto56.coresystem.common.msg.util.db.IPage;
import cn.com.yto56.coresystem.common.msg.util.db.QueryPage;
import cn.com.yto56.coresystem.common.msg.util.db.dao.annotation.JdbcDao;
import cn.com.yto56.coresystem.logic.msg.mc.dao.IClientInfoDao;
import cn.com.yto56.coresystem.logic.msg.mc.domain.ClientInfoBean;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * Created by wangrui on 2017/5/4.
 */
@JdbcDao(dataSource = "msg_database")
public class ClientInfoDaoImpl extends BaseJdbcUtil implements IClientInfoDao {
    @Override
    public IPage<ClientInfoBean> search(ClientInfoBean clientInfoBean, QueryPage queryPage) {
        StringBuffer sb = new StringBuffer();
        sb.append("select * from " + SysConst.T_MSG_MC_CLIENTINFO + " where status='VALID' ");
        String yto_msg_sysinfo_id = clientInfoBean.getYto_msg_sysinfo_id();
        String yto_msg_userid = clientInfoBean.getYto_msg_userid();
        String onlinestate = clientInfoBean.getOnlinestate();
        String clienttype = clientInfoBean.getClienttype();
        List<Object> args = new ArrayList<Object>();
        if (StringUtils.isNotBlank(yto_msg_sysinfo_id)) {
            sb.append(" and yto_msg_sysinfo_id=?");
            args.add(yto_msg_sysinfo_id);
        }
        if (StringUtils.isNotBlank(yto_msg_userid)) {
            sb.append(" and yto_msg_userid=?");
            args.add(yto_msg_userid);
        }
        if (StringUtils.isNotBlank(onlinestate)) {
            sb.append(" and onlinestate=?");
            args.add(onlinestate);
        }
        if (StringUtils.isNotBlank(clienttype)) {
            sb.append(" and clienttype=?");
            args.add(clienttype);
        }

        Map<String, String> sortMap = new HashMap<String, String>();
        sortMap.put("create_time", DESC);
        return (IPage<ClientInfoBean>) this.queryByPageMS(sb.toString(), args, queryPage.getPageSize(), queryPage.getPageIndex(), sortMap, new RowMapper<ClientInfoBean>() {
            public ClientInfoBean mapRow(ResultSet rs, int arg1) throws SQLException {
                ClientInfoBean clientInfoBean = new ClientInfoBean();
                clientInfoBean.setId(rs.getInt("id"));
                clientInfoBean.setYto_msg_sysinfo_id(rs.getString("yto_msg_sysinfo_id"));
                clientInfoBean.setYto_msg_userid(rs.getString("yto_msg_userid"));
                clientInfoBean.setConnectiontime(rs.getString("connectiontime"));
                clientInfoBean.setLogintime(rs.getString("logintime"));
                clientInfoBean.setUnconnectiontime(rs.getString("unconnectiontime"));
                clientInfoBean.setConnectionnum(rs.getInt("connectionnum"));
                clientInfoBean.setLoginnum(rs.getInt("loginnum"));
                clientInfoBean.setClienttype(rs.getString("clienttype"));
                clientInfoBean.setOnlinestate(rs.getString("onlinestate"));
                return clientInfoBean;
            }
        });
    }

    @Override
    public boolean searchForUpdate(ClientInfoBean clientInfoBean) {
        return checkForUpdate(SysConst.T_MSG_MC_CLIENTINFO, DataTypeChangeTool.beanToMap(clientInfoBean));
    }

    @Override
    public int delete(ClientInfoBean clientInfoBean) {
        int result = 0;
        Map setMap = new HashMap();
        setMap.put("status", "INVALID");
        setMap.put("update_time", new Date());
        Map map = DataTypeChangeTool.beanToMap(clientInfoBean);
        result = updateData(SysConst.T_MSG_MC_CLIENTINFO, setMap, map, false);
        return result;
    }

    @Override
    public int update(ClientInfoBean set, ClientInfoBean where) {
        int result = 0;
        Map setMap = DataTypeChangeTool.beanToMap(set);
        Map whereMap = DataTypeChangeTool.beanToMap(where);
        result = updateData(SysConst.T_MSG_MC_CLIENTINFO, setMap, whereMap, false);
        return result;
    }

    @Override
    public int add(ClientInfoBean clientInfoBean) {
        int result = 0;
        Map map = DataTypeChangeTool.beanToMap(clientInfoBean);
        result = insertData(SysConst.T_MSG_MC_CLIENTINFO, map);
        return result;
    }

}
